package com.demo.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.vc.demo.crypto.AlgorithmUtil;
import com.vc.demo.crypto.SecurityUtil;
import com.vc.demo.model.OpenResult;
import com.vc.demo.model.Payload;
import com.vc.demo.model.RestResult;
import com.vc.demo.utils.TimeUtils;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class 请求数据加密 {

    @Test
    public void clientEncrypt(){

        try {
            Payload payload = new Payload();
            payload.setRequestId(1234l+"");
            payload.setTimestamp(TimeUtils.format_yyyyMMddHHmmss(new Date()));
            payload.setApiKey("CD8D9B55376B5612B23AC282FF3EE6BC");
            payload.setEncryptedRandomKey(null);
            payload.setSignature(null);
            Map<String, String> bizData = new HashMap<>();
            bizData.put("type", "1");
            bizData.put("validSeconds", "10");
            bizData.put("extId", "100");
            bizData.put("desc", "请在图片中点击数字5");
            bizData.put("bgImgBase64", "");
            payload.setBizData(bizData);
            System.out.println("原始数据=>" + JSON.toJSONString(payload));
            String text = SecurityUtil.encrypt(JSON.toJSONString(payload), Key.tenantPublicKey);
            System.out.println("加密后=>" + text + "\n");
            // 签名
            String text1 = SecurityUtil.sign(text, Key.severPrivateKey);
            System.out.println("签名后=>" + text1);


        }catch (Exception e){

        }
    }


    @Test
    public void decrypt(){
        try {
            //服务端的加密
            JSONObject xyObject = new JSONObject();
            xyObject.put("xPer","0.25");
            xyObject.put("xPer","0.25");
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(xyObject);
            OpenResult openResult = OpenResult.success("", RestResult.success(jsonArray.toJSONString()));
            System.out.println("原始数据=>" + JSON.toJSONString(openResult));
            String text1 = SecurityUtil.severEncryptAndSign(openResult, Key.tenantPublicKey,Key.severPrivateKey);
            System.out.println("加密签名后=>" + text1 + "\n");
            //客户端解密
            boolean ok = SecurityUtil.verify(text1,  Key.severPublicKey);
            System.out.println("签名验证=>" + ok);

            System.out.println("加密=>" + openResult.getData().toString());

            String randomKey = AlgorithmUtil.decryptWithRSA(openResult.getEncryptedRandomKey(), Key.tenantPrivateKey);
            System.out.println("randomKey=>" +randomKey);

            String bizData = AlgorithmUtil.decryptWithAES(openResult.getData().toString(), randomKey);
//            String randomKey = AlgorithmUtil.decryptWithRSA(openResult.getData().toString(), Key.tenantPrivateKey);
            System.out.println("解密后的数据=>" + bizData);
        }catch (Exception e){
            System.err.println(e.getLocalizedMessage());
        }


    }

}
